package com.monai.aidati.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class FanoutProducer {
    private static final String EXCHANGE_NAME = "fanout-exchange";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setPassword("admin");
        factory.setUsername("admin");
        factory.setHost("localhost");
        try(Connection connection=factory.newConnection();
            Channel channel=connection.createChannel()){
            channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
            Scanner scanner=new Scanner(System.in);
            while(scanner.hasNext()){
                String message=scanner.nextLine();
                channel.basicPublish(EXCHANGE_NAME,"",null,message.getBytes("utf-8"));
                System.out.println(" [x] Sent '" + message + "'");
            }
        }

    }
}
